﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Dynamic;
using System.Data.SqlClient;
using System.Configuration;
using Model;
using Common;
namespace DAL
{
    public class AdminDAL
    {
        public static int Save(AdminInfo info)
        {
            string sql = string.Format("insert into [admin]([UserName],[UserPwd],[RealName],[Power],[Menu1],[Menu2],[Menu3],[Menu4],[Menu5],[Menu6],[Menu7],[Menu8],[We1],[We2],[We3],[We4],[We5],[We6],[We7],[Notice],[Intro],[Cult],[Environment],[Honor],[Copyright1],[Copyright2],[Copyright3],[Copyright4],[Copyright5],[CreateDate]) values(@UserName,@UserPwd,@RealName,@Power,@Menu1,@Menu2,@Menu3,@Menu4,@Menu5,@Menu6,@Menu7,@Menu8,@We1,@We2,@We3,@We4,@We5,@We6,@We7,@Notice,@Intro,@Cult,@Environment,@Honor,@Copyright1,@Copyright2,@Copyright3,@Copyright4,@Copyright5,@CreateDate);select @@identity;");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@UserName", info.UserName),
                new SqlParameter("@UserPwd", info.UserPwd),
                new SqlParameter("@RealName", info.RealName),
                new SqlParameter("@Power", "menu-1,all-1,menu-10,btns-10-1,btns-10-2,btns-10-3,menu-12,btns-12-1,btns-12-2,btns-12-3,menu-23,all-23,menu-3,btns-3-1,btns-3-2,btns-3-3,menu-24,all-24,menu-11,btns-11-1,btns-11-2,btns-11-3,menu-13,btns-13-1,btns-13-2,btns-13-3,btns-13-6,menu-20,btns-20-1,btns-20-2,btns-20-3,menu-25,all-25,menu-14,btns-14-1,btns-14-2,btns-14-3,menu-9,all-9,menu-15,btns-15-1,btns-15-2,btns-15-3,menu-26,all-26,menu-16,menu-17,menu-22,menu-18,menu-5,all-5,menu-21"),
                new SqlParameter("@Menu1", "1&1&公司介绍"),
                new SqlParameter("@Menu2", "2&1&产品中心"),
                new SqlParameter("@Menu3", "3&0&公司环境"),
                new SqlParameter("@Menu4", "4&0&资质荣誉"),
                new SqlParameter("@Menu5", "5&0&企业文化"),
                new SqlParameter("@Menu6", "6&0&联系我们"),
                new SqlParameter("@Menu7", "7&1&留言反馈"),
                new SqlParameter("@Menu8", "8&1&地理位置"),
                new SqlParameter("@We1", "1&1&公司环境"),
                new SqlParameter("@We2", "2&1&资质荣誉"),
                new SqlParameter("@We3", "3&1&公司介绍"),
                new SqlParameter("@We4", "4&1&企业文化"),
                new SqlParameter("@We5", "5&1&联系我们"),
                new SqlParameter("@We6", "6&1&留言反馈"),
                new SqlParameter("@We7", "7&1&地理位置"),
                new SqlParameter("@Notice", "1&1&6&1&最新资讯"),
                new SqlParameter("@Intro", "2&1&公司介绍"),
                new SqlParameter("@Cult", "3&1&企业文化"),
                new SqlParameter("@Environment", "4&1&4&公司环境"),
                new SqlParameter("@Honor", "5&1&4&资质荣誉"),
                new SqlParameter("@Copyright1", "互联网一站式解决方案"),
                new SqlParameter("@Copyright2", "Copyright&nbsp;icoi&nbsp;2014-2018"),
                new SqlParameter("@Copyright3", "/image/logo.png"),
                new SqlParameter("@Copyright4", "铭恩科技提供技术支持&nbsp;&nbsp;"),
                new SqlParameter("@Copyright5", "0371-60905321"),
                new SqlParameter("@CreateDate", info.CreateDate)
            };
            return DataConvert.ToInt(CommonDAL.ExecuteScalar(sql, paras));
        }
        public static int Update(AdminInfo info)
        {
            string sql = string.Format("update [admin] set [UserName]=@UserName,[UserPwd]=@UserPwd,[RealName]=@RealName where [ID]=@ID");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@UserName", info.UserName),
                new SqlParameter("@UserPwd", info.UserPwd),
                new SqlParameter("@RealName", info.RealName),
                new SqlParameter("@ID", info.ID)
            };
            return CommonDAL.ExecuteNoneQuery(sql, paras) ? info.ID : 0;
        }
        public static int UpdateContact(AdminInfo info)
        {
            string sql = string.Format("update [admin] set [ComName]=@ComName,[ContactName]=@ContactName,[Phone]=@Phone,[Mobile]=@Mobile,[QQ]=@QQ,[WeiXin]=@WeiXin,[Sina]=@Sina,[Email]=@Email,[Lat]=@Lat,[Lng]=@Lng,[Address]=@Address where [ID]=@ID");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@ComName", info.ComName),
                new SqlParameter("@ContactName", info.ContactName),
                new SqlParameter("@Phone", info.Phone),
                new SqlParameter("@Mobile", info.Mobile),
                new SqlParameter("@QQ", info.QQ),
                new SqlParameter("@WeiXin", info.WeiXin),
                new SqlParameter("@Sina", info.Sina),
                new SqlParameter("@Email", info.Email),
                new SqlParameter("@Lat", info.Lat),
                new SqlParameter("@Lng", info.Lng),
                new SqlParameter("@Address", info.Address),
                new SqlParameter("@ID", info.ID)
            };
            return CommonDAL.ExecuteNoneQuery(sql, paras) ? info.ID : 0;
        }
        public static int UpdateCopyright(AdminInfo info)
        {
            string sql = string.Format("update [admin] set [Copyright1]=@Copyright1,[Copyright2]=@Copyright2,[Copyright3]=@Copyright3,[Copyright4]=@Copyright4,[Copyright5]=@Copyright5 where [ID]=@ID");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@Copyright1", info.Copyright1),
                new SqlParameter("@Copyright2", info.Copyright2),
                new SqlParameter("@Copyright3", info.Copyright3),
                new SqlParameter("@Copyright4", info.Copyright4),
                new SqlParameter("@Copyright5", info.Copyright5),
                new SqlParameter("@ID", info.ID)
            };
            return CommonDAL.ExecuteNoneQuery(sql, paras) ? info.ID : 0;
        }
        public static int UpdateCominfo(AdminInfo info)
        {
            string sql = string.Format("update [admin] set [ComName]=@ComName,[Lat]=@Lat,[Lng]=@Lng,[Address]=@Address where [ID]=@ID");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@ComName", info.ComName),
                new SqlParameter("@Lat", info.Lat),
                new SqlParameter("@Lng", info.Lng),
                new SqlParameter("@Address", info.Address),
                new SqlParameter("@ID", info.ID)
            };
            return CommonDAL.ExecuteNoneQuery(sql, paras) ? info.ID : 0;
        }
        public static AdminInfo FindOneByID(int id)
        {
            DataRow row = CommonDAL.FindOneByID(id, "admin");
            return SimpleData(row);
        }


        public static AdminInfo Login(string username, string userpwd)
        {
            DataRow row = CommonDAL.GetDataRow("select * from admin where username='" + username + "' and userpwd='" + userpwd + "'");
            return SimpleData(row);
        }
        
        public static List<AdminInfo> FindPage(string keyword, string begindate, string enddate, int startindex, int pagesize, string order, string desc, out int count)
        {
            StringBuilder filter = new StringBuilder();
            if (!string.IsNullOrEmpty(keyword))
            {
                filter.AppendFormat(" and [admin].Name like '%{0}%'", keyword);
            }
            if (!string.IsNullOrEmpty(begindate) && !string.IsNullOrEmpty(enddate))
            {
                filter.AppendFormat(" and [admin].CreateDate between '{0}' and '{1}'", DataConvert.ToDateTime(begindate).ToString("yyyy-MM-dd 00:00:00"), DataConvert.ToDateTime(enddate).ToString("yyyy-MM-dd 23:59:59"));
            }
            DataTable dt = CommonDAL.FindPage(startindex, pagesize, order, desc, filter, "[admin]", out count);
            return ListSimpleData(dt);
        }
        public static List<AdminInfo> FindListByRole(int role)
        {
            DataTable dt = CommonDAL.GetDataTable(string.Format("select * from [admin] where Role={0}",role));
            return ListSimpleData(dt);
        }
        private static AdminInfo SimpleData(DataRow dr)
        {
            AdminInfo info = new AdminInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.UserName = DataConvert.ToString(dr["UserName"]);
                info.UserPwd = DESEncrypt.Decrypt(DataConvert.ToString(dr["UserPwd"]));
                info.RealName = DataConvert.ToString(dr["RealName"]);
                info.ComName = DataConvert.ToString(dr["ComName"]);
                info.Introduce = DataConvert.ToString(dr["Introduce"]);
                info.Culture = DataConvert.ToString(dr["Culture"]);
                info.ContactName = DataConvert.ToString(dr["ContactName"]);
                info.Phone = DataConvert.ToString(dr["Phone"]);
                info.Mobile = DataConvert.ToString(dr["Mobile"]);
                info.QQ = DataConvert.ToString(dr["QQ"]);
                info.WeiXin = DataConvert.ToString(dr["WeiXin"]);
                info.Sina = DataConvert.ToString(dr["Sina"]);
                info.Email = DataConvert.ToString(dr["Email"]);
                info.Lat = DataConvert.ToFloat(dr["Lat"]);
                info.Lng = DataConvert.ToFloat(dr["Lng"]);
                info.Address = DataConvert.ToString(dr["Address"]);
                info.Power = DataConvert.ToString(dr["Power"]);
                info.Menu1 = DataConvert.ToString(dr["Menu1"]);
                info.Menu2 = DataConvert.ToString(dr["Menu2"]);
                info.Menu3 = DataConvert.ToString(dr["Menu3"]);
                info.Menu4 = DataConvert.ToString(dr["Menu4"]);
                info.Menu5 = DataConvert.ToString(dr["Menu5"]);
                info.Menu6 = DataConvert.ToString(dr["Menu6"]);
                info.Menu7 = DataConvert.ToString(dr["Menu7"]);
                info.Menu8 = DataConvert.ToString(dr["Menu8"]);
                info.We1 = DataConvert.ToString(dr["We1"]);
                info.We2 = DataConvert.ToString(dr["We2"]);
                info.We3 = DataConvert.ToString(dr["We3"]);
                info.We4 = DataConvert.ToString(dr["We4"]);
                info.We5 = DataConvert.ToString(dr["We5"]);
                info.We6 = DataConvert.ToString(dr["We6"]);
                info.We7 = DataConvert.ToString(dr["We7"]);
                info.Notice = DataConvert.ToString(dr["Notice"]);
                info.Intro = DataConvert.ToString(dr["Intro"]);
                info.Cult = DataConvert.ToString(dr["Cult"]);
                info.Environment = DataConvert.ToString(dr["Environment"]);
                info.Honor = DataConvert.ToString(dr["Honor"]);
                info.Copyright1 = DataConvert.ToString(dr["Copyright1"]).Replace("icoi", "©");
                info.Copyright2 = DataConvert.ToString(dr["Copyright2"]).Replace("icoi", "©");
                info.Copyright3 = DataConvert.ToString(dr["Copyright3"]).Replace("icoi", "©");
                info.Copyright4 = DataConvert.ToString(dr["Copyright4"]).Replace("icoi", "©");
                info.Copyright5 = DataConvert.ToString(dr["Copyright5"]).Replace("icoi", "©");
                info.CreateDate = DataConvert.ToDateTime(dr["CreateDate"]);

                info.Token = DESEncrypt.Encrypt(info.ID.ToString());
            }
            return info;
        }
        private static AdminInfo AllData(DataRow dr)
        {
            AdminInfo info = new AdminInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.UserName = DataConvert.ToString(dr["UserName"]);
                info.UserPwd = DataConvert.ToString(dr["UserPwd"]);
                info.RealName = DataConvert.ToString(dr["RealName"]);
                info.ComName = DataConvert.ToString(dr["ComName"]);
                info.Introduce = DataConvert.ToString(dr["Introduce"]);
                info.Culture = DataConvert.ToString(dr["Culture"]);
                info.ContactName = DataConvert.ToString(dr["ContactName"]);
                info.Phone = DataConvert.ToString(dr["Phone"]);
                info.Mobile = DataConvert.ToString(dr["Mobile"]);
                info.QQ = DataConvert.ToString(dr["QQ"]);
                info.WeiXin = DataConvert.ToString(dr["WeiXin"]);
                info.Sina = DataConvert.ToString(dr["Sina"]);
                info.Email = DataConvert.ToString(dr["Email"]);
                info.Lat = DataConvert.ToFloat(dr["Lat"]);
                info.Lng = DataConvert.ToFloat(dr["Lng"]);
                info.Address = DataConvert.ToString(dr["Address"]);
                info.Power = DataConvert.ToString(dr["Power"]);
                info.Menu1 = DataConvert.ToString(dr["Menu1"]);
                info.Menu2 = DataConvert.ToString(dr["Menu2"]);
                info.Menu3 = DataConvert.ToString(dr["Menu3"]);
                info.Menu4 = DataConvert.ToString(dr["Menu4"]);
                info.Menu5 = DataConvert.ToString(dr["Menu5"]);
                info.Menu6 = DataConvert.ToString(dr["Menu6"]);
                info.Menu7 = DataConvert.ToString(dr["Menu7"]);
                info.Menu8 = DataConvert.ToString(dr["Menu8"]);
                info.We1 = DataConvert.ToString(dr["We1"]);
                info.We2 = DataConvert.ToString(dr["We2"]);
                info.We3 = DataConvert.ToString(dr["We3"]);
                info.We4 = DataConvert.ToString(dr["We4"]);
                info.We5 = DataConvert.ToString(dr["We5"]);
                info.We6 = DataConvert.ToString(dr["We6"]);
                info.We7 = DataConvert.ToString(dr["We7"]);
                info.Notice = DataConvert.ToString(dr["Notice"]);
                info.Intro = DataConvert.ToString(dr["Intro"]);
                info.Cult = DataConvert.ToString(dr["Cult"]);
                info.Environment = DataConvert.ToString(dr["Environment"]);
                info.Honor = DataConvert.ToString(dr["Honor"]);
                info.Copyright1 = DataConvert.ToString(dr["Copyright1"]).Replace("icoi", "©");
                info.Copyright2 = DataConvert.ToString(dr["Copyright2"]).Replace("icoi", "©");
                info.Copyright3 = DataConvert.ToString(dr["Copyright3"]).Replace("icoi", "©");
                info.Copyright4 = DataConvert.ToString(dr["Copyright4"]).Replace("icoi", "©");
                info.Copyright5 = DataConvert.ToString(dr["Copyright5"]).Replace("icoi", "©");
                info.CreateDate = DataConvert.ToDateTime(dr["CreateDate"]);

                info.Token = DESEncrypt.Encrypt(info.ID.ToString());
            }
            return info;
        }
        private static List<AdminInfo> ListSimpleData(DataTable dt)
        {
            List<AdminInfo> list = new List<AdminInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(SimpleData(row));
            }
            return list;
        }
        private static List<AdminInfo> ListAllData(DataTable dt)
        {
            List<AdminInfo> list = new List<AdminInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(AllData(row));
            }
            return list;
        }

    }
}
